package com.controller;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.Users;
import com.service.LogService;
import com.service.UsersService;

@Controller
public class UsersController {
	UsersService us = new UsersService();
	LogService ls = new LogService();

	/**
	 * 登陆
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			HttpServletResponse response) {
		// System.out.println(account+" "+password);
		Users result = us.queryone(account, password);
		// System.out.println(result);
		if ("0".equals(result.getCounter())) {
			return "ERR";
		}
		ls.add(result.getId(), "0", "登陆");
		response.addCookie(new Cookie("users", result.getId()));
		return "OK";
	}

	/**
	 * 退出登陆
	 */
	@RequestMapping(value = "/querynull.action", method = RequestMethod.POST)
	@ResponseBody
	public String querynull(
			@CookieValue(value = "users", defaultValue = "") String userid,
			HttpServletResponse response) {
		Map<String, Object> map = us.lastId();
		ls.add(userid, map.get("id").toString(), "退出登录");
		response.addCookie(new Cookie("users", null));
		return "OK";
	}
	
	/**
	 * 获取用户昵称
	 */
	@RequestMapping(value = "/querynick.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> querynick(
			@CookieValue(value = "users", defaultValue = "") String userid,
			HttpServletResponse response) {
		Map<String, Object> user=us.querynick(userid);
		//System.out.println(user);
		return user;
	}

}
